package com.meritit.product.spi;

import com.sun.portal.container.EntityID;
import com.sun.portal.container.PortletLang;
import com.sun.portal.container.PortletType;
import com.sun.portal.container.PortletWindowContext;
import com.sun.portal.container.PortletWindowContextException;
import com.sun.portal.container.service.EventHolder;
import com.sun.portal.container.service.PublicRenderParameterHolder;
import com.sun.portal.container.service.policy.DistributionType;

import javax.portlet.PortletPreferences;
import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.ResourceBundle;

public class PortletWindowContextImpl implements PortletWindowContext {

    private static final String DEFAULT_DESKTOP_URL = "/portal";
    private static final String DEFAULT_LOCALE_STRING = "zh_CN";


    private HttpServletRequest request;

    private String desktopURL = DEFAULT_DESKTOP_URL;

    private String localeString = DEFAULT_LOCALE_STRING;

    private String authenticationType;

    private String userRepresentation;

    private List<String> roles = new ArrayList<String>();

    private Map<String, String> userInfo = new HashMap<String, String>();

    private Map<String, Object> properties = new HashMap<String, Object>();

    private List<String> markupTypes = new ArrayList<String>();

    private String description;
    private String shortTitle;
    private String title;
    private List<String> keywords;
    private String displayName;
    private Map<String, String> roleMap = new HashMap<String, String>();
    private Map<String, String> userInfoMap = new HashMap<String, String>();
    private String portletName;
    private List<EntityID> portletWindows = new ArrayList<EntityID>();
    private String producerEntityID;
    private String portletID;
    private String portletWindowTitle;
    private PortletPreferences portletPreferences;
    private PortletLang portletLang;
    private String userID;
    private static final String DEFAULT_CONTENT_TYPE = "text/html";

    public PortletWindowContextImpl(String userID) {
        this.userID = userID;
    }

    public PortletWindowContextImpl() {
    }

    @Override
    public void init(HttpServletRequest request) {
        this.request = request;
    }

    @Override
    public String getDesktopURL(HttpServletRequest request) {
        return desktopURL;
    }

    @Override
    public String getDesktopURL(HttpServletRequest request, String query, boolean escape) {
        return desktopURL;
    }

    @Override
    public String getLocaleString() {
        return localeString;
    }

    @Override
    public String getContentType() {
        //FIXME default to text/html
        return DEFAULT_CONTENT_TYPE;
    }

    @Override
    public String encodeURL(String url) {
        return url;
    }

    @Override
    public boolean isAuthless(HttpServletRequest request) {
        return false;
    }

    @Override
    public String getAuthenticationType() {
        return this.authenticationType;
    }

    @Override
    public String getUserRepresentation() {
        return this.userRepresentation;
    }

    @Override
    public List<String> getRoles() {
        return this.roles;
    }

    @Override
    public Map<String, String> getUserInfo() {
        return this.userInfo;
    }

    @Override
    public Object getProperty(String name) {
        return this.properties.get(name);
    }

    @Override
    public void setProperty(String name, Object value) {
        properties.put(name, value);
    }

    @Override
    public List<String> getMarkupTypes(String portletName) throws PortletWindowContextException {
        return this.markupTypes;
    }

    @Override
    public String getDescription(String portletName, String desiredLocale) throws PortletWindowContextException {
        return this.description;
    }

    @Override
    public String getShortTitle(String portletName, String desiredLocale) throws PortletWindowContextException {
        return this.shortTitle;
    }

    @Override
    public String getTitle(String portletName, String desiredLocale) throws PortletWindowContextException {
        return this.title;
    }

    @Override
    public List<String> getKeywords(String portletName, String desiredLocale) throws PortletWindowContextException {
        return this.keywords;
    }

    @Override
    public String getDisplayName(String portletName, String desiredLocale) throws PortletWindowContextException {
        return this.displayName;
    }

    @Override
    public Map<String, String> getRoleMap(String portletName) throws PortletWindowContextException {
        return this.roleMap;
    }

    @Override
    public Map<String, String> getUserInfoMap(String portletName) throws PortletWindowContextException {
        return this.userInfoMap;
    }

    @Override
    public String getPortletName(String portletWindowName) throws PortletWindowContextException {
        return this.portletName;
    }

    @Override
    public List<EntityID> getPortletWindows(PortletType portletType, DistributionType distributionType) throws PortletWindowContextException {
        return this.portletWindows;
    }

    @Override
    public EntityID getEntityID(String portletWindowName) throws PortletWindowContextException {
        return portletWindows.get(0);
    }

    @Override
    public String getProducerEntityID(String portletWindowName) throws PortletWindowContextException {
        return this.producerEntityID;
    }

    @Override
    public String getConsumerID(String portletWindowName) throws PortletWindowContextException {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public String getPortletID(String portletWindowName) throws PortletWindowContextException {
        return this.portletID;
    }

    @Override
    public String getPortletHandle(String portletWindowName) throws PortletWindowContextException {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public void setPortletHandle(String portletWindowName, String portletHandle) throws PortletWindowContextException {
        //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public String getPortletWindowTitle(String portletWindowName, String desiredLocale) throws PortletWindowContextException {
        return this.portletWindowTitle;
    }

    @Override
    public PortletPreferences getPreferences(String portletWindowName, ResourceBundle bundle, boolean isReadOnly) throws PortletWindowContextException {
        return this.portletPreferences;
    }

    @Override
    public void store() throws PortletWindowContextException {
        throw new UnsupportedOperationException("");
    }

    @Override
    public PortletLang getPortletLang(String portletWindowName) throws PortletWindowContextException {
        return this.portletLang;
    }

    @Override
    public EventHolder verifySupportedPublishingEvent(EntityID portletEntityId, EventHolder eventHolder) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<EventHolder> getSupportedPublishingEventHolders(EntityID portletEntityId) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public EventHolder verifySupportedProcessingEvent(EntityID portletEntityId, EventHolder eventHolder) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<EventHolder> getSupportedProcessingEventHolders(EntityID portletEntityId) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public Map<String, String> verifySupportedPublicRenderParameters(EntityID portletEntityId, List<PublicRenderParameterHolder> publicRenderParameterHolders) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }

    @Override
    public List<PublicRenderParameterHolder> getSupportedPublicRenderParameterHolders(EntityID portletEntityId, Map<String, String[]> renderParameters) {
        return null;  //To change body of implemented methods use File | Settings | File Templates.
    }
}
